﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Common;
using Basic.Model.Entity;
using Basic.Model.EntityManager;

namespace Basic.Business
{
    public class BasicHashTableAdmin
    {
        /// <summary>
        /// 获取系统基本配置的信息
        /// </summary>
        /// <returns></returns>
        public static void AddHashTable(string hashTableName, string HashTableKey, string HashTableValue)
        {
            ISession session = null;
            ITransaction trans = null;
            BasicHashTable basicHashTable = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                basicHashTable = new BasicHashTable();
                basicHashTable.hashTableName = hashTableName;
                basicHashTable.hashTableKey = HashTableKey;
                basicHashTable.hashTableValue = HashTableValue;
                BasicHashTableManager.Add(session,basicHashTable);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }
        /// <summary>
        /// 更新一个哈希表数据
        /// </summary>
        /// <param name="hashTableName"></param>
        /// <param name="hashTable"></param>
        public static void UpdateHashTableValue(string hashTableName, Hashtable hashTable)
        {
            ISession session = null;
            ITransaction trans = null;
            BasicHashTable basicHashTable = null;
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                basicHashTable = BasicHashTableManager.Get(session, hashTableName);
                IDictionaryEnumerator hash = hashTable.GetEnumerator();
                basicHashTable.hashTableKey = "";
                basicHashTable.hashTableValue = "";
                while (hash.MoveNext())
                {
                    basicHashTable.hashTableKey += hash.Key.ToString() + "|";
                    basicHashTable.hashTableValue += hash.Value.ToString() + "|";
                }
                basicHashTable.hashTableKey = basicHashTable.hashTableKey.Substring(0, basicHashTable.hashTableKey.Length - 1);
                basicHashTable.hashTableValue = basicHashTable.hashTableValue.Substring(0, basicHashTable.hashTableValue.Length - 1);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
        }

        /// <summary>
        /// 获取系统基本配置的信息
        /// </summary>
        /// <returns></returns>
        public static Hashtable GetHashTable(string hashTableName)
        {
            ISession session = null;
            ITransaction trans = null;
            BasicHashTable basicHashTable = null;
            Hashtable hashTable = new Hashtable();
            try
            {
                session = NHibernateHelper.OpenSession();
                trans = session.BeginTransaction();
                basicHashTable = BasicHashTableManager.Get(session, hashTableName);
                trans.Commit();
            }
            catch
            {
                if (trans != null)
                    trans.Rollback();
                throw;
            }
            finally
            {
                if (session != null)
                    session.Close();
            }
            string[] hashTableKey = basicHashTable.hashTableKey.Split('|');// System.StringSplitOptions.RemoveEmptyEntries);
            string[] hashTableValue = basicHashTable.hashTableValue.Split('|');
            for (int i = 0; i < hashTableKey.Length; i++)
            {
                hashTable.Add(hashTableKey[i], hashTableValue[i]);
            }
            return hashTable;
        }
    }
}
